package com.base.invoke.okhttp.callback;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.base.invoke.okhttp.response.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.base.invoke.okhttp.util.FileUtil;
import okhttp3.Call;

/**
 * @desc 文件下载回调结果返回
 * @author coolpig
 * @date 2019-08-28
 */
public abstract class DownFilesCallback extends Callback{
	public static Logger logger = LoggerFactory.getLogger(DownFilesCallback.class);
	private String filePat;

	public DownFilesCallback(){}
	public DownFilesCallback(String filePat){
		this.filePat = filePat;
	}

	@Override
	public void doResponse(Call call, Response response, int id) {
		try {
			if(filePat != null && filePat.length() > 0){
				File file=new File(filePat);
				FileUtil.saveContent(response.body().bytes(),file);
				onSuccess(call, file, id);
			}else{
				onSuccess(call, response.body().byteStream(), id);
			}
		} catch (IOException ex) {
			logger.error(ex.getMessage(), ex);
		}
	}

	public void onSuccess(Call c ,File f, int id) {
		
	}

	public void onSuccess(Call c, InputStream in, int id) {
		
	}
}
